#ifndef TRANSPLOTHELPER_H
#define TRANSPLOTHELPER_H

class TransPlotHelper {

public:
    /**
     * @brief getSeriesPosByTime. Find the closest position in the series by given "time" parameter. Used to optimize the performance.
     * @param series
     * @param Time
     * @return
     */
    static int getSeriesPosByTime(const TransSeries &series, int Time)
    {
        const QVector<Transaction> v_trans = series.getSeries();
        if ( v_trans.size() < 3 ) return 0;
        int start = 0;
        int end = v_trans.size() - 1;
        int target = Time;
        int pos = 0;
        do {

            pos = (start + end) >> 1;
            int current0 = v_trans[pos].getTime();
            int current1 = v_trans[pos + 1].getTime();

            if ( (target <= current1) && (target >= current0) ) break;

            if ( target > current1 ) {
                start = pos;
            }
            else {
                end = pos;
            }

        } while ( (pos != 0) && (pos != v_trans.size()-1) );

        return pos;
    }
};

#endif // TRANSPLOTHELPER_H
